package com.sk.mylibrary;


import android.hardware.input.InputManager;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import android.view.MotionEvent.PointerProperties;

public class InputUtils {
    private static final String TAG = "InputUtils";
    public InputManager inputManager;
    private PointerProperties[] properties = createPointerPropertiesArray(2);
    private PointerCoords[] pointerCoords = createPointerCoordsArray(2);
    private int aValue;

    InputUtils() {
        //IBinder imBinder = ServiceManager.getService("input");
        //inputManager = IInputManager.Stub.asInterface(imBinder);

        inputManager = InputManager.getInstance();


    }

    public static KeyEvent getKeyEvent(int action, int code) {
        return KeyEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), action, code, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, null);
    }

/*
    public  void sendKeyDown(int inputSource, float x, float y) {
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x, y, 1.0f);

    }
    public  void sendKeyUp(int inputSource, float x, float y) {
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x, y, 1.0f);

    }*/

	
    /*public  void sendTap(int inputSource, float x, float y) {
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x, y, 1.0f);
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x, y, 0.0f);
    }
    
    public  void sendTapB(int inputSource, float x, float y) {
        long now = SystemClock.uptimeMillis();
        injectMotionEventB(inputSource, MotionEvent.ACTION_DOWN, now, x, y, 1.0f);
        injectMotionEventB(inputSource, MotionEvent.ACTION_UP, now, x, y, 0.0f);
    }
   
    public  void sendSwipe(int inputSource, float x1, float y1, float x2, float y2) {
        final int NUM_STEPS = 11;
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0f);
        for (int i = 1; i < NUM_STEPS; i++) {
            float alpha = (float) i / NUM_STEPS;
            injectMotionEvent(inputSource, MotionEvent.ACTION_MOVE, now, lerp(x1, x2, alpha),
                    lerp(y1, y2, alpha), 1.0f);
        }
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x1, y1, 0.0f);
    }
    
    public  void sendSwipeUnUP(int inputSource, float x1, float y1, float x2, float y2,long now) {
    	final int NUM_STEPS = 11;
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0f);
        for (int i = 1; i < NUM_STEPS; i++) {
            float alpha = (float) i / NUM_STEPS;
            injectMotionEvent(inputSource, MotionEvent.ACTION_MOVE, now, lerp(x1, x2, alpha),
                    lerp(y1, y2, alpha), 1.0f);
        }
    }
    public  void sendSwipeUP(int inputSource, float x1, float y1, float x2, float y2,long now) {
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x1, y1, 0.0f);
    }
    
    public  void sendSwipeStart(float x1, float y1, float x2, float y2,long now) {
    	final int NUM_STEPS = 11;
        injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0f);
        for (int i = 1; i < NUM_STEPS; i++) {
            float alpha = (float) i / NUM_STEPS;
            injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_MOVE, now, lerp(x1, x2, alpha),
                    lerp(y1, y2, alpha), 1.0f);
        }
    }
    public  void sendSwipeStop(float x1, float y1, float x2, float y2,long now) {
        injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_UP, now, x1, y1, 0.0f);
    }
    
    public void sendSwipeDown(float x1, float y1,long now)
    {
    	injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0f);
    }
    
    public  void sendSwipeStartB(float x1, float y1, float x2, float y2,long now) {
    	final int NUM_STEPS = 11;
        injectMotionEventB(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0f);
        for (int i = 1; i < NUM_STEPS; i++) {
            float alpha = (float) i / NUM_STEPS;
            injectMotionEventB(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_MOVE, now, lerp(x1, x2, alpha),
                    lerp(y1, y2, alpha), 1.0f);
        }
    }
    public  void sendSwipeStopB(float x1, float y1, float x2, float y2,long now) {
        injectMotionEventB(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_UP, now, x1, y1, 0.0f);
    }
    
    public  void sendMove(int inputSource, float dx, float dy) {
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_MOVE, now, dx, dy, 0.0f);
    }

    public  void injectKeyEvent(KeyEvent event) {
        Log.i(TAG, "injectKeyEvent: " + event);
        try {
        inputManager.injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);//INJECT_INPUT_EVENT_MODE_ASYNC;INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
        }catch (RemoteException e) {
            System.err.println(e.toString());
            return;
        }
    }*/

    public static MotionEvent.PointerProperties[] createPointerPropertiesArray(int paramInt) {
        PointerProperties[] arrayOfPointerProperties = new PointerProperties[paramInt];
        int i = 0;
        while (i < paramInt) {
            arrayOfPointerProperties[i] = new PointerProperties();
            i += 1;
        }
        return arrayOfPointerProperties;
    }


    public static PointerCoords[] createPointerCoordsArray(int paramInt) {
        PointerCoords[] arrayOfPointerCoords = new PointerCoords[paramInt];
        int i = 0;
        while (i < paramInt) {
            arrayOfPointerCoords[i] = new PointerCoords();
            i += 1;
        }
        return arrayOfPointerCoords;
    }


    public void injectMotionEvent(int inputSource,int keyid, int action, int pointCount, float x, float y) {

        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis();
       /* pp2.id = 1;
        pp2.toolType = MotionEvent.TOOL_TYPE_FINGER;*/
        // properties[1] = pp2;
 /*       pc2.x = 444;
        pc2.y = 555;
        pc2.pressure = 100;
        pc2.size = 1;*/
        // pointerCoords[0] = pc1;
        //  pointerCoords[1] = pc2;
        MotionEvent event;
        MotionEvent event1;
        // if (action==MotionEvent.ACTION_DOWN) {

        // for (int i = 0;i < pointCount;i++){
        // PointerProperties pp1 = new PointerProperties();
        //properties[pointCount-1].clear();
              /*  pp1.id = 1;
                pp1.toolType = MotionEvent.TOOL_TYPE_FINGER;
                properties[0] = pp1;*/
        // }
        //PointerProperties pp[] = properties[pointCount-1].createArray(1);
        //   PointerProperties pp =  new   PointerProperties();
          //properties[pointCount-1].clear();

/*        properties[pointCount - 1].id = keyid;
        properties[pointCount - 1].toolType = MotionEvent.TOOL_TYPE_FINGER;
        //  properties[pointCount-1]=pp[pointCount-1];
        //   System.err.println("InjectServer down =="+pointCount);
        // PointerCoords pc[]=pointerCoords[pointCount-1].createArray(1);
        //  pointerCoords[pointCount-1].clear();
        pointerCoords[pointCount - 1].x = x;
        pointerCoords[pointCount - 1].y = y;
        pointerCoords[pointCount - 1].pressure = 1F;
        pointerCoords[pointCount - 1].size = 1F;*/

        // pointerCoords[pointCount-1] = pc[pointCount-1];
           /* PointerCoords pc1 =  new PointerCoords();
            //pointerCoords[pointCount-1].clear();
            pc1.x = x;
            pc1.y = y;
            pc1.pressure = 1F;
            pc1.size = 1F;
            pointerCoords[0] = pc1;*/

        System.err.println(" pointCount ==" + pointCount);
        System.err.println("InjectServer down ==" + (((0xff & (keyid-1)) << 8 |  MotionEvent.ACTION_POINTER_DOWN)+action));
        if (pointCount == 1) {

            properties[pointCount-1].clear();
            properties[pointCount - 1].id = keyid;
            properties[pointCount - 1].toolType = MotionEvent.TOOL_TYPE_FINGER;
            pointerCoords[pointCount-1].clear();
            pointerCoords[pointCount - 1].x = x;
            pointerCoords[pointCount - 1].y = y;
            pointerCoords[pointCount - 1].pressure = 1F;
            pointerCoords[pointCount - 1].size = 1F;
            aValue = action;
        } else {

            properties[keyid-1].clear();
            properties[keyid - 1].id = keyid;
            properties[keyid - 1].toolType = MotionEvent.TOOL_TYPE_FINGER;
            pointerCoords[keyid-1].clear();
            pointerCoords[keyid - 1].x = x;
            pointerCoords[keyid - 1].y = y;
            pointerCoords[keyid - 1].pressure = 1F;
            pointerCoords[keyid - 1].size = 1F;
            aValue = (((0xff & (keyid-1)) << 8 |  MotionEvent.ACTION_POINTER_DOWN)+action);
        }
       // if (pointCount == 1) {
            event = MotionEvent.obtain(downTime, eventTime,
                    aValue, pointCount, properties, pointerCoords,
                    0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);
            inputManager.injectInputEvent(event,
                    InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
      /*  }else {
            System.err.println("InjectServer down =1=" + pointCount);
            event1 = MotionEvent.obtain(downTime, eventTime,
                    0x100 | MotionEvent.ACTION_POINTER_DOWN, 2, properties,
                    pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);
            inputManager.injectInputEvent(event1,
                    InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
        }*/
/*            event2 = MotionEvent.obtain(downTime, eventTime,
                    0x200|MotionEvent.ACTION_POINTER_DOWN, 3, properties,
                    pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);
            event3 = MotionEvent.obtain(downTime, eventTime,
                    0x300|MotionEvent.ACTION_POINTER_DOWN, 4, properties,
                    pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);*/
  /*      }else{


            PointerProperties[] properties = new PointerProperties[pointCount];
            properties[pointCount-1].clear();
            properties[pointCount-1].id = pointCount;
            properties[pointCount-1].toolType = MotionEvent.TOOL_TYPE_FINGER;

            PointerCoords[] pointerCoords = new PointerCoords[pointCount];
            pointerCoords[pointCount-1].clear();
            pointerCoords[pointCount-1].x = x;
            pointerCoords[pointCount-1].y = y;
            pointerCoords[pointCount-1].pressure = 1F;
            pointerCoords[pointCount-1].size = 1F;*/

   /*          event = MotionEvent.obtain(downTime, eventTime,
                     0x100|action, pointCount, properties, pointerCoords,
                     0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);*/


        /*     event1 = MotionEvent.obtain(downTime, eventTime,
                     0x100|MotionEvent.ACTION_POINTER_UP, 2, properties,
                    pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);*/
        /*    event2 = MotionEvent.obtain(downTime, eventTime,
                    0x200|MotionEvent.ACTION_POINTER_UP, 3, properties,
                    pointerCoords, 0, 0, 1.0f,1.0f, 0, 0, inputSource, 0);
            event3 = MotionEvent.obtain(downTime, eventTime,
                    0x300|MotionEvent.ACTION_POINTER_UP, 4, properties,
                    pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, inputSource, 0);*/
            //}
            // event.setSource(inputSource);
       /* event1.setSource(inputSource);
        event2.setSource(inputSource);
        event3.setSource(inputSource);*/

            //  Log.i(TAG, "injectMotionEvent: " + event);
            try {




/*            inputManager.injectInputEvent(event2,
                    InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
             inputManager.injectInputEvent(event3,
                   InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);*/

            } catch (Exception e) {
                System.err.println(e.toString());
                return;
            }
        }
    /*public  void injectMotionEventB(int inputSource, int action, long when, float x, float y, float pressure) {
        final float DEFAULT_SIZE = 1.0f;
        final int DEFAULT_META_STATE = 0;
        final float DEFAULT_PRECISION_X = 1.0f;
        final float DEFAULT_PRECISION_Y = 1.0f;
        final int DEFAULT_DEVICE_ID = 0;
        final int DEFAULT_EDGE_FLAGS = 0;
        MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,
                DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,
                DEFAULT_EDGE_FLAGS);
        event.setSource(inputSource);
        Log.i(TAG, "injectMotionEvent: " + event);
        try {
        inputManager.injectInputEvent(event,
        		InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
        }catch (RemoteException e) {
            System.err.println(e.toString());
            return;
        }
    }*/
//    public static final float lerp(float a, float b, float alpha) {
//        return (b - a) * alpha + a;
//    }

    }